package com.esoon.tools;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class Tools {
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int screenWidth = wm.getDefaultDisplay().getWidth();//屏幕宽度
        return screenWidth;
    }

    private static ProgressDialog progressDialog = null;

    public static void showProgressDialog(Context context, String msg) {
        progressDialog = ProgressDialog.show(context, "",
                msg, true, false);

        progressDialog.show();

    }

    public static void dismissProgressDialog() {
        if (progressDialog != null)
            progressDialog.dismiss();
        progressDialog = null;

    }





    // 屏幕的宽度, 高度.
    public static DisplayMetrics getScreenSize(Activity act) {

        DisplayMetrics displayMetrics = new DisplayMetrics();
        act.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics;
    }

    /**
     * 检测网络是否连通...
     *
     * @param context
     * @return true: 连通, false: No connected..
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * wifi 是否可用.
     *
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mWiFiNetworkInfo != null) {
                return mWiFiNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 手机移动网络是否可用.
     *
     * @param context
     * @return
     */
    public static boolean isMobileConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mMobileNetworkInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mMobileNetworkInfo != null) {
                return mMobileNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 获取网络的连接方式
     *
     * @param context
     * @return -i1: 没任何连接.
     */
    public static int getConnectedType(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
                return mNetworkInfo.getType();
            }
        }
        return -1;
    }

    /**
     * 读写配置文件.
     *
     * @param context
     * @param key
     * @param sdata
     */
    public static void SaveConfigData(Context context, String key, String sdata) {

        SharedPreferences.Editor editor = context.getSharedPreferences("lock", context.MODE_PRIVATE).edit();
        editor.putString(key, sdata);
        editor.commit();
    }


    public static String getConfigData(Context context, String key) {

        SharedPreferences read = context.getSharedPreferences("lock", context.MODE_PRIVATE);
        String value = read.getString(key, "");
        return value;
    }


}
